﻿using System;
using UnityEngine;

public class AIPatrol : MonoBehaviour
{
    private Vector3 lastSelfPos;
    public AILook look;
    public AIMove move;
    private float stoppedTime;
    private Vector3 targetPos;

    private void Start()
    {
        this.targetPos = this.selfPos;
    }

    private void Update()
    {
        if ((this.move != null) && !this.move.hasTarget)
        {
            if (this.lastSelfPos == this.selfPos)
            {
                this.stoppedTime += Time.deltaTime;
            }
            if ((this.stoppedTime > 2f) || (Vector3.Distance(this.selfPos, this.targetPos) < 0.5f))
            {
                this.targetPos = this.selfPos + ((Vector3) (UnityEngine.Random.insideUnitSphere.SetY(0f) * 5f));
                this.stoppedTime = 0f;
            }
            this.move.direction = this.targetPos - this.selfPos;
            if (this.look != null)
            {
                this.look.direction = this.move.direction;
            }
            Debug.DrawLine(this.targetPos, this.selfPos);
            this.lastSelfPos = this.selfPos;
        }
    }

    private Vector3 selfPos
    {
        get
        {
            return ((this.move == null) ? base.transform.position : this.move.transform.position);
        }
    }
}

